package com.g2.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.g2.admin.model.to.AuthorizeTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.g2.admin.model.po.AuthPermitRolePo;
import com.g2.admin.dao.mapper.IAuthPermitRoleMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class AuthPermitRoleService extends ServiceImpl<IAuthPermitRoleMapper, AuthPermitRolePo> implements IAuthPermitRoleService
	@Autowired
	private IAuthPermitRoleMapper authpermitroleMapper;
	
	public AuthPermitRolePo getAuthpermitrole(long id) {
		return authpermitroleMapper.selectById(id);
	}

	public void delAuthpermitrole(long id) {
		authpermitroleMapper.deleteById(id);
	}

	public void addAuthpermitrole(AuthPermitRolePo authpermitrole) {
		authpermitroleMapper.insert(authpermitrole);
	}
	
	public void updateAuthpermitrole(AuthPermitRolePo authpermitrole) {
		authpermitroleMapper.updateById(authpermitrole);
	}
	@Transactional(rollbackFor = Exception.class)
	public void savePermitBatch(AuthorizeTo authorizeTo) {
		// 先删除此用户的所有权限，再重新添加
		authpermitroleMapper.delete(getQw(uid));
		saveBatch(permitUsers);
	}


	private QueryWrapper<AuthPermitRolePo> getQw(String roleCode) {
		QueryWrapper<AuthPermitRolePo> qw = new QueryWrapper<>();
		qw.lambda().eq(AuthPermitRolePo::getUserId, uid)
				.orderByDesc(AuthPermitRolePo::getSort);
		return qw;
	}
}
